<template>
  <div class="query-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>查询单据</span>
        </div>
      </template>

      <el-form :inline="true" :model="queryForm" class="search-form">
        <el-form-item label="单据编号">
          <el-input
            v-model="queryForm.documentNo"
            placeholder="请输入单据编号"
          />
        </el-form-item>
        <el-form-item label="单据类型">
          <el-select v-model="queryForm.type" placeholder="请选择单据类型">
            <el-option label="入库单" value="in" />
            <el-option label="出库单" value="out" />
            <el-option label="移库单" value="transfer" />
          </el-select>
        </el-form-item>
        <el-form-item label="日期范围">
          <el-date-picker
            v-model="queryForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="documentNo" label="单据编号" />
        <el-table-column prop="type" label="单据类型" />
        <el-table-column prop="date" label="创建日期" />
        <el-table-column prop="status" label="状态">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button link type="primary" @click="handleView(scope.row)"
              >查看</el-button
            >
            <el-button link type="primary" @click="handlePrint(scope.row)"
              >打印</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";

const queryForm = reactive({
  documentNo: "",
  type: "",
  dateRange: [],
});

const tableData = ref([
  {
    documentNo: "IN202403200001",
    type: "入库单",
    date: "2024-03-20",
    status: "已完成",
  },
  {
    documentNo: "OUT202403200001",
    type: "出库单",
    date: "2024-03-20",
    status: "处理中",
  },
]);

const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(100);

const getStatusType = (status) => {
  const statusMap = {
    已完成: "success",
    处理中: "warning",
    已取消: "info",
  };
  return statusMap[status] || "info";
};

const handleSearch = () => {
  // 实现查询逻辑
  console.log("查询条件：", queryForm);
};

const handleReset = () => {
  queryForm.documentNo = "";
  queryForm.type = "";
  queryForm.dateRange = [];
};

const handleView = (row) => {
  console.log("查看单据：", row);
};

const handlePrint = (row) => {
  console.log("打印单据：", row);
};

const handleSizeChange = (val) => {
  pageSize.value = val;
  // 重新加载数据
};

const handleCurrentChange = (val) => {
  currentPage.value = val;
  // 重新加载数据
};
</script>

<style scoped>
.query-container {
  padding: 20px;
}

.search-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
